<!DOCTYPE html>
<!--第一条注释 -->
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>8.1.2</title>
    <style>
        frame {
            border: 1px solid #000;
        }
    </style>
</head>

<!-- <frameset rows="160,*">
        <frame src="frame.htm" name="topFrame">
            <frameset cols="50%,50%">
                <frame src="anotherframe.htm" name="leftFrame">
                <frame src="yetanotherframe.htm" name="rightFrame">
            </frameset>
    </frameset> -->
<!-- <frameset rows="100,*">
            <frame src="frame.htm" name="topFrame">
            <frameset cols="50%,50%">
                <frame src="anotherframe.htm" name="leftFrame">
                <frame src="anotherframeset.htm" name="rightFrame">
            </frameset>
        </frameset> -->
<div id="bar2">123</div>
<div id="bar">
    <p class="foo">
        Hello World!
    </p>
    <p class="foo">
        Hello World!
        <span>OK</span>
    </p>
    <p class="foo">
        Hello World!
        <b>OBJ</b>
    </p>
</div>
<ul id="bar3">
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>
<script>
    /* function Person() {}
                var friend = new Person();
                Person.prototype.name = 'hello'
                Person.prototype.sayHi = function() {
                    console.log(friend.name);
                };
                friend.sayHi(); //"hi"（没有问题！） */

    /* function Person() {}
            var friend = new Person();
            Person.prototype.sayName = function() {
                console.log('friend.name');
            };
            Person.prototype = {
                constructor: Person,
                name: 'Nicholas',
                age: 29,
                job: 'Software Engineer',
                sayName: function() {
                    console.log(this.name);
                }
            };
            friend.sayName(); */

    /* function Person() {}
            var friend = new Person();
            Person.prototype = {
                sayName: function() {
                    console.log('hi');
                }
            };
            friend.sayName(); */

    /* function Person(name, age, job) {
                //创建要返回的对象
                var o = new Object();
                //可以在这里定义私有变量和函数
                //添加方法
                o.sayName = function() {
                    console.log(name);
                };
                //返回对象
                return o;
            }
            var friend = Person('Nicholas', 29, 'Software Engineer');
            friend.sayName(); //"Nicholas" */

    /*                 function createPerson(name, age, job){
        var o = new Object();
        o.name = name;
        o.age = age;
        o.job = job;
        o.sayName = function(){
            console.log(this.name);
        };
        return o;
    }
    var person1 = createPerson("Nicholas", 29, "Software Engineer");
    var person2 = createPerson("Greg", 27, "Doctor");
    console.log(person1 instanceof Object); //true
    console.log(person1 instanceof createPerson); //false 判断person1是否是createPerson的实例
    console.log(person2 instanceof Object); //true
    console.log(person2 instanceof createPerson); //false */
    /* function SuperType(){
        this.property = true;
    }

    SuperType.prototype.getSuperValue = function(){
        return this.property;
    };

    function SubType(){
        this.subproperty = '123';
    }

    //继承了SuperType
    SubType.prototype = new SuperType();

    SubType.prototype.getSubValue = function (){
        return this.subproperty;
    };
    //重写超类型中的方法
    SubType.prototype.getSuperValue = function (){
        return false;
    };
    var instance = new SubType();
    console.log(instance.getSuperValue()); //true
    console.log(instance.getSubValue()); //false */
    /* function SuperType(){
        this.colors = ["red", "blue", "green"];
    }
        console.log(this);

    function SubType(){
        //继承了SuperType
        console.log(JSON.stringify(this));
        console.log(111111);
        SuperType.call(this);
        console.log(JSON.stringify(this));
    }

    var instance1 = new SubType();
    instance1.colors.push("black");
    console.log(instance1.colors); //"red,blue,green,black"

    var instance2 = new SubType();
    console.log(instance2.colors); //"red,blue,green" */
    /* function SuperType(name){
        this.name = name;
        this.colors = ["red", "blue", "green"];
    }

    SuperType.prototype.sayName = function(){
        console.log(this.name);
    };

    function SubType(name, age){
        //继承属性
        SuperType.call(this, name);
        this.age = age;
    }
    //继承方法
    SubType.prototype = new SuperType();
    SubType.prototype.constructor = SubType;
    SubType.prototype.sayAge = function(){
        console.log(this.age);
    };

    var instance1 = new SubType("Nicholas", 29);
    instance1.colors.push("black");
    console.log(instance1.colors); //"red,blue,green,black"
    instance1.sayName(); //"Nicholas";
    instance1.sayAge(); //29

    var instance2 = new SubType("Greg", 27);
    console.log(instance2.colors); //"red,blue,green"
    instance2.sayName(); //"Greg";
    instance2.sayAge(); //27

    console.log(instance1 instanceof Object); //true
    console.log(instance1 instanceof SuperType); //true
    console.log(instance1 instanceof SubType); //true
    console.log(instance2 instanceof Object); //true
    console.log(instance2 instanceof SuperType); //true
    console.log(instance2 instanceof SubType); //true */

    /* function object(o){
        function F(){}
        F.prototype = o;
        return new F();
    }

    var person = {
        name: "Nicholas",
        friends: ["Shelby", "Court", "Van"]
    };

    var anotherPerson = object(person);
    anotherPerson.name = "Greg";
    anotherPerson.friends.push("Rob");
    console.log(anotherPerson.name);

    var yetAnotherPerson = object(person);
    yetAnotherPerson.name = "Linda";
    yetAnotherPerson.friends.push("Barbie");

    console.log(person.name);
    console.log(person.friends); //"Shelby,Court,Van,Rob,Barbie" */

    /* var person = {
        name: "Nicholas",
        friends: ["Shelby", "Court", "Van"]
    };

    var anotherPerson = Object.create(person, {
        name: {
            value: "Greg"
        }
    });

    console.log(person.name); //"Nicholas"
    console.log(anotherPerson.name); //"Greg"
     */
    /* var person = {
        name: "Nicholas",
        friends: ["Shelby", "Court", "Van"]
    };

    var anotherPerson = Object.create(person);
    anotherPerson.name = "Greg";
    anotherPerson.friends.push("Rob");

    var yetAnotherPerson = Object.create(person);
    yetAnotherPerson.name = "Linda";
    yetAnotherPerson.friends.push("Barbie");

    console.log(person.friends); //"Shelby,Court,Van,Rob,Barbie"
    console.log(person.name); //"Nicholas"
    console.log(anotherPerson.name); //"Greg"
    console.log(yetAnotherPerson.name); //"Linda" */
    /* function object(o){
        function F(){}
        F.prototype = o;
        return new F();
    }

    function inheritPrototype(subType, superType){
        var prototype = object(superType.prototype); //创建对象
        prototype.constructor = subType; //增强对象
        subType.prototype = prototype; //指定对象
    }
    function SuperType(name){
        this.name = name;
        this.colors = ["red", "blue", "green"];
    }

    SuperType.prototype.sayName = function(){
        console.log(this.name);
    };

    function SubType(name, age){
        SuperType.call(this, name);
        this.age = age;
    }

    inheritPrototype(SubType, SuperType);
        SubType.prototype.sayAge = function(){
        console.log(this.age);
    };

    var instance1 = new SubType("Nicholas", 29);
    instance1.colors.push("black");
    console.log(instance1.colors); //"red,blue,green,black"
    instance1.sayName(); //"Nicholas";
    instance1.sayAge(); //29

    var instance2 = new SubType("Greg", 27);
    console.log(instance2.colors); //"red,blue,green"
    instance2.sayName(); //"Greg";
    instance2.sayAge(); //27

    console.log(instance1 instanceof Object); //true
    console.log(instance1 instanceof SuperType); //true
    console.log(instance1 instanceof SubType); //true */

    /* function createComparisonFunction(propertyName) {
        return function(object1, object2){
            var value1 = object1[propertyName];
            var value2 = object2[propertyName];

            if (value1 < value2){
                return -1;
            } else if (value1 > value2){
                return 1;
            } else {
                return 0;
            }
        };
    }
    var data = [{name: "Zachary", age: 28}, {name: "Nicholas", age: 29}];
    data.sort(createComparisonFunction("name"));
    console.log(data[0].name); //Nicholas
    data.sort(createComparisonFunction("age"));
    console.log(data[0].name); //Zachary */


    /* function createFunctions(){
        var result = new Array();

        for (var i=0; i < 10; i++){
            result[i] = function(num){
                return function(){
                    return num;
                };
            }(i);
        }

        return result;
    }

    var foo = createFunctions();
    console.table(foo[0](), foo[1](), foo[2]()); */

    // "use strict";

    /* var name = "The Window";

    var object = {
        name : "My Object",
        getNameFunc : function(){
            return function(){
                return this.name;
            };
        }
    };

    console.log(object.getNameFunc()()); */
    /*     function outputNumbers(count) {
            for (var i = 0; i < count; i++) {
                console.log(i);
            }
            var i = 9; //重新声明变量
            console.log(i); //计数
        }
        outputNumbers(5) */

    /*
        var text = "https://www.youtube.com/embed/asdaqwe123?12312123";
        var pattern = /(.)embed/g;
        if (pattern.test(text)) {
            console.log(RegExp.rightContext);
            var a = RegExp.rightContext;
            var b = a.indexOf('?');
            console.log(b);
            var c = a.slice(0, b);
            console.log(c);
        } */
    /*     function MyObject() {
            //私有变量和私有函数
            var privateVariable = 10;

            function privateFunction() {
                return false;
            }
            //特权方法
            this.publicMethod = function() {
                privateVariable++;
                console.log(privateVariable);
                return privateFunction();
            };
        }
        var foo = new MyObject();
        var bar = foo.publicMethod();
        console.log(bar); */



    /*     (function() {
            //私有变量和私有函数
            var privateVariable = 10;

            function privateFunction() {
                return false;
            }

            //构造函数
            MyObject = function() {};

            //公有/特权方法
            MyObject.prototype.publicMethod = function() {
                privateVariable++;
                return privateFunction();
            };
        })();
        var foo = new MyObject();
        var bar = foo.publicMethod();
        console.log(bar); */


/*     var singleton = function() {
        //私有变量和私有函数
        var privateVariable = 10;

        function privateFunction() {
            return false;
        }
        //特权/公有方法和属性
        return {
            publicProperty: true,
            publicMethod: function() {
                privateVariable++;
                return privateFunction();
            }
        };
    }();
    var foo = singleton.publicProperty;
    console.log(foo);
    var bar = singleton.publicMethod();
    console.log(bar); */

/*     function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}

var person = {
    name: "Nicholas",
    friends: ["Shelby", "Court", "Van"]
};

var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");

console.log(person.name); //"Nicholas"
console.log(person.friends); //"Shelby,Court,Van,Rob,Barbie"
console.log(anotherPerson.name); //"Greg"
console.log(yetAnotherPerson.name); //"Linda"
var person = {
    name: "Nicholas",
    friends: ["Shelby", "Court", "Van"]
};

var anotherPerson = Object.create(person, {
    name: {
        value: "Greg"
    }
});

console.log(person.name); //"Nicholas"
console.log(anotherPerson.name); //"Greg" */


/*
function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}

function inheritPrototype(subType, superType){
    var prototype = object(superType.prototype); //创建对象
    prototype.constructor = subType; //增强对象
    subType.prototype = prototype; //指定对象
}

inheritPrototype(SubType, SuperType);
function SuperType(name){
    this.name = name;
    this.colors = ["red", "blue", "green"];
}

SuperType.prototype.sayName = function(){
    console.log(this.name);
};

function SubType(name, age){
    SuperType.call(this, name);
    this.age = age;
}


SubType.prototype.sayAge = function(){
    console.log(this.age);
};

var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29

var instance2 = new SubType("Greg", 27);
console.log(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27

console.log(instance1 instanceof Object); //true
console.log(instance1 instanceof SuperType); //true
console.log(instance1 instanceof SubType); //true */

// "use strict"
var name = "The Window";

var object = {
    name : "My Object",
    getNameFunc : function(){
        return function(){
            return this.name;
        };
    }
};

var a = object.getNameFunc()

console.log(a());

</script>

</html>